package com.linuxgt.android.spapp.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.HashMap;
import java.util.Map;

/**
 * 软件参数保存服务
 * Created by liuteng on 2017/5/16.
 */

public class PreferencesService {

    /**
     * 应用上下文
     */
    private Context context;

    /**
     * 软件参数保存服务构造器
     * @param context
     */
    public PreferencesService(Context context) {
        this.context = context;
    }

    /**
     * 保存软件设置参数
     * @param name
     * @param age
     */
    public void save(String name, Integer age) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(getClass().getSimpleName(), Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putString("name", name);
        editor.putInt("age", age);
        editor.commit();
    }

    /**
     * 获取软件设置参数
     * @return
     */
    public Map<String, String> getPreferences() {
        Map<String, String> params = new HashMap<>();
        SharedPreferences sharedPreferences = context.getSharedPreferences(getClass().getSimpleName(), Context.MODE_PRIVATE);
        params.put("name", sharedPreferences.getString("name", ""));
        params.put("age", String.valueOf(sharedPreferences.getInt("age", 0)));
        return params;
    }
}
